package com.stx.demo;

import com.stx.handler.Handler;
import com.stx.handler.Looper;
import com.stx.handler.Message;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;

class MainUI{

    private static final int MSG_MAIN_THREAD_2_THREAD = 1000;
    private static final int MSG_THREAD_2_THREAD = 1001;

    private JFrame frame = new JFrame();
    private JButton sendBtn1 = new JButton("主程序发消息到线程");
    private JButton sendBtn2 = new JButton("线程发消息到线程");
    private JButton sendBtn3 = new JButton("退出消息队列");

    private ActionListener buttonListener = new ButtonActionListener();

    private Handler handler;

    private Runnable runnable1;
    private Thread   thread1;

    private static final Object lockObj = new Object();



    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.layoutUI();
    }


    public MainUI(){
        initThread();
    }


    public void layoutUI() {
        frame.setTitle("Demo");
        frame.setSize(750, 600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width / 2;
        int screenHeight = screenSize.height / 2;
        int height = frame.getHeight();
        int width = frame.getWidth();
        frame.setLocation(screenWidth - width / 2, screenHeight - height / 2);
        frame.setResizable(false);

        sendBtn1.setBounds(40, 60, 200, 25);
        sendBtn1.addActionListener(buttonListener);
        frame.add(sendBtn1);

        sendBtn2.setBounds(260, 60, 200, 25);
        sendBtn2.addActionListener(buttonListener);
        frame.add(sendBtn2);

        sendBtn3.setBounds(480, 60, 200, 25);
        sendBtn3.addActionListener(buttonListener);
        frame.add(sendBtn3);


        frame.setVisible(true);
    }

    private class ButtonActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            long time1 = System.currentTimeMillis();
            Object source = e.getSource();
            if (sendBtn1 == source) {
                onSendBtn1();
            }else if(sendBtn2 == source){
                onSendBtn2();
            }else if (sendBtn3 == source){
                onSendBtn3();
            }
        }
    }

    private void onSendBtn1(){
        if (handler != null){
            Message msg = handler.obtainMessage(MSG_MAIN_THREAD_2_THREAD, "主线程发送到子线程");
            handler.sendMessage(msg);
        }
    }

    private void onSendBtn2(){
        Runnable r = new Runnable() {
            @Override
            public void run() {
                if (handler != null){
                    Message msg = handler.obtainMessage(MSG_THREAD_2_THREAD, "子线程发送到子线程");
                    handler.sendMessage(msg);
                }
            }
        };
        Thread th = new Thread(r);
        th.start();
    }

    private void onSendBtn3(){
        Looper looper = Looper.getLooper(thread1);
        if (looper != null){
            looper.quitSafely();
        }else{
            System.out.println("looper is null...");
        }
    }

    private void initThread(){
        runnable1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("enter thread1...");
                Looper.prepare();
                initHandler();
                Looper.loop();
                System.out.println("exit thread1...");
            }
        };
        thread1 = new Thread(runnable1);
        thread1.start();
        System.out.println("initThread...");
    }

    private void initHandler(){
        handler = new Handler(){
            public void handleMessage(Message msg){
                onRecvHandleMessage(msg);
            }
        };
    }

    private void onRecvHandleMessage(Message msg){
        String strMsg = "Recv Msg, msg id: " + msg.what + " , msg content: " + msg.obj;
        System.out.println(strMsg);
    }


}






